package dao;
import java.sql.*;

public class JDBCconection {
	private static Connection conn=null;
	private static PreparedStatement ps=null;
	private static ResultSet rs=null;
	static {
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	public static Connection getConn() {
		try {
			conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/online_bookstore?serverTimezone=UTC","root","123456");
		}catch(SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}
	public static void close(Connection conn, PreparedStatement ps, ResultSet rs) {
		if(rs!=null) {
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			
		}
		if(ps!=null) {
			try {
				ps.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if(conn!=null) {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	public static boolean updata(String sql){
		boolean flag=false;
		conn=getConn();
		try {
			ps=conn.prepareStatement(sql);
			int n=ps.executeUpdate();
			if(n>0){
				flag=true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
		
	}
	public static ResultSet getData(String sql){
		conn=getConn();
		try {
			ps=conn.prepareStatement(sql);
			rs=ps.executeQuery();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rs;
	}
}
